package com.zhangbo.myapplication.activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.zhangbo.myapplication.R;
import com.zhangbo.myapplication.aidl.Book;
import com.zhangbo.myapplication.aidl.IBookManager;
import com.zhangbo.myapplication.service.BookManagerService;

import java.util.ArrayList;
import java.util.List;

public class BookManagerActivity extends AppCompatActivity {

    private static final String Tag = BookManagerActivity.class.getSimpleName();

    private List<Book> bookList = new ArrayList<>();

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e(Tag, "onServiceConnected");
            IBookManager bookManager = IBookManager.Stub.asInterface(service);
            try {
                bookList.clear();
                bookList.addAll(bookManager.getBookList());
                Log.e(Tag, "query book list, list type:" + bookList.getClass().getCanonicalName());
                Log.e(Tag, "query book list : " + bookList.size());
                bookListAdapter.notifyDataSetChanged();

                Book book = new Book(3, "android开发艺术探索");
                bookManager.addBook(book);

                List<Book> newList = bookManager.getBookList();
                Log.e(Tag, "query book list:" + newList.size());

            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(Tag, "onServiceDisconnected");
        }
    };
    private ListView lv_book_list;
    private BookListAdapter bookListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(Tag, "onCreate");
        setContentView(R.layout.activity_book_manager);
        lv_book_list = (ListView) findViewById(R.id.lv_book_list);
        bookListAdapter = new BookListAdapter(BookManagerActivity.this, R.layout.book_item, bookList);
        lv_book_list.setAdapter(bookListAdapter);

        setTitle(Tag);
        Intent intent = new Intent(this, BookManagerService.class);
        bindService(intent, connection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onDestroy() {
        Log.e(Tag, "onDestroy");
        unbindService(connection);
        super.onDestroy();
    }


    public class BookListAdapter extends ArrayAdapter<Book> {

        private BookListAdapter(@NonNull Context context, int resource, @NonNull List<Book> objects) {
            super(context, resource, objects);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            Book book = bookList.get(position);
            View view;
            if (convertView == null) {
                view = LayoutInflater.from(BookManagerActivity.this).inflate(R.layout.book_item, parent, false);
            } else {
                view = convertView;
            }

            TextView tvBookId = (TextView) view.findViewById(R.id.tv_book_id);
            TextView tvBookName = (TextView) view.findViewById(R.id.tv_book_name);

            tvBookId.setText(" " + book.getBookId());
            tvBookName.setText(book.getBookName());

            return view;
        }
    }
}
